package com.yzd.learnjava.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import com.yzd.learnjava.util.Print;

public class AtomicityTest implements Runnable {
	private int i = 0;
	public int getValue() {
		return i;
	}
	
	private synchronized void evenIncrement() {
		i++;
		i++;
	}
	
	public void run() {
		while(true) {
			evenIncrement();
		}
	}
	
	public static void main(String[] args) throws Exception {
		ExecutorService exec = Executors.newCachedThreadPool();
		AtomicityTest at = new AtomicityTest();
		exec.submit(at);
		//TimeUnit.SECONDS.sleep(1);
		while(true) {
			int val = at.getValue();
			if(val % 2 != 0) {
				Print.print(val);
				System.exit(0);
			}
		}
	}
}
